package miku.firstgame.ui.card
{
	import com.saia.starlingPunk.SPEntity;
	import com.saia.starlingPunk.SPMask;

	import feathers.controls.Label;

	import miku.firstgame.entity.Bomb;
	import miku.firstgame.model.User;
	import miku.firstgame.model.gold.card.ToolCard;
	import miku.firstgame.model.gold.card.ToolType;

	import starling.display.Quad;

	public class UserSeat extends SPEntity
	{
		public static const WIDTH:int = 60;
		public static const HEIGHT:int = 60;

		private var back:Quad;
		private var nameLabel:Label;

		public var user:User;

		private var _selected:Boolean;

		private var lightTool:Quad;
		private var carTool:Quad;
		private var shovelTool:Quad;

		public function UserSeat()
		{
			super(0, 0, "userSeat");
		}

		override public function added():void
		{
			super.added();
			setupGraphic();
		}

		private function setupGraphic():void
		{
			back = new Quad(WIDTH, HEIGHT, 0xafd8fa);
			addChildAt(back, 0);
			updateUser();
		}

		override public function removed():void
		{
			removeChildren(0, -1, true);
			super.removed();
		}

		override public function update():void
		{
			super.update();

		}

		public function set selected(value:Boolean):void
		{
			if (value)
			{
				back.color = 0x6f98aa;
			}
			else
			{
				back.color = 0xafd8fa;
			}
			_selected = value;
		}

		public function get selected():Boolean
		{
			return _selected;
		}

		public function attachUser(user:User):void
		{
			this.user = user;
			if (null == nameLabel)
			{
				nameLabel = new Label();
				addChild(nameLabel);

				lightTool = new Quad(10, 10, 0xff0000);
				lightTool.x = 5;
				lightTool.y = 40 + 5;
				lightTool.visible = false;
				addChild(lightTool);

				carTool = new Quad(10, 10, 0xff0000);
				carTool.x = 5 + 20;
				carTool.y = 40 + 5;
				carTool.visible = false;
				addChild(carTool);

				shovelTool = new Quad(10, 10, 0xff0000);
				shovelTool.x = 5 + 20 + 20;
				shovelTool.y = 40 + 5;
				shovelTool.visible = false;
				addChild(shovelTool);
			}
		}

		public function applyToolCard(card:ToolCard):void
		{
			if (card.isDestroy)
			{
				if (user.hasTool(card.type)) //如果有了这张卡
				{
					user.removeTools(card.type);
				}
			}
			else
			{
				if (!user.hasTool(card.type))
				{
					user.getTool(card.type);
				}
			}
			refreshTool();
		}

		public function canApplyToolCard(card:ToolCard):Boolean
		{
			if (card.isDestroy)
			{
				if (user.hasTool(card.type)) //如果有了这张卡
				{
					return true;
				}
			}
			else
			{
				if (!user.hasTool(card.type))
				{
					return true;
				}
			}
			return false;
		}

		private function refreshTool():void
		{
			if (user)
			{
				if (!user.hasTool(ToolType.LIGHT))
				{
					lightTool.visible = true;
				}
				else
				{
					lightTool.visible = false;
				}
				if (!user.hasTool(ToolType.CAR))
				{
					carTool.visible = true;
				}
				else
				{
					carTool.visible = false;
				}
				if (!user.hasTool(ToolType.SHOVEL))
				{
					shovelTool.visible = true;
				}
				else
				{
					shovelTool.visible = false;
				}
			}
			else
			{
				lightTool.visible = false;
				carTool.visible = false;
				shovelTool.visible = false;
			}
		}

		private function updateUser():void
		{
			if (user && nameLabel)
			{
				nameLabel.text = user.name;
				nameLabel.validate();
				nameLabel.x = (WIDTH - nameLabel.width) >> 1;
				nameLabel.y = (HEIGHT - nameLabel.height) >> 1;
			}
		}

		public function get haveUser():Boolean
		{
			if (user)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		public function cancelAttachUser():void
		{
			if (this.user)
			{
				this.user = null;
				nameLabel.text = "";
			}
		}
	}
}
